/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jefb.entity.DestinationEntity;
import org.jefb.entity.FileEntity;
import org.jefb.service.ICommonPersistenceService;
import org.jefb.web.model.DirectoryMananagerModel;
import org.jefb.web.model.SyncModel;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Button;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;

@SuppressWarnings("serial")
public class SyncController extends GenericForwardComposer implements
		Serializable {

	// UI components
	private SyncModel model;

	protected Button assignDestinationButton;

	protected Grid assignedDestinationsGrid;
	protected Button removeAssignedDestination;
	
	private ICommonPersistenceService commonPersistenceService;
	private DirectoryMananagerModel directoryManagerModel;


	public SyncController() {
	}

	public void init() {
		reloadLists();
	}

	private void reloadLists() {
		// fetch file entity representing common sync directory
		FileEntity syncDir = new FileEntity();
		syncDir.setName("/csync");
		List<FileEntity> found = getCommonPersistenceService()
				.findAllByExample(syncDir);
		if (found.isEmpty()) {
			getCommonPersistenceService().persistEntity(syncDir);
			getModel().setRootFileEntity(syncDir);
		} else {
			getModel().setRootFileEntity(found.get(0));
		}

		if (getModel().getAssignedDestinationsListModel() == null) {
			List<DestinationEntity> assignedDestsList = new ArrayList<DestinationEntity>();
			assignedDestsList.addAll(getModel().getRootFileEntity()
					.getDestinations());
			getModel().setAssignedDestinationsListModel(
					new BindingListModelList(assignedDestsList, false));
		} else {
			getModel().getAssignedDestinationsListModel().clear();
			getModel().getAssignedDestinationsListModel().addAll(
					getModel().getRootFileEntity().getDestinations());
		}

		List<DestinationEntity> allDestinations = getCommonPersistenceService()
				.findAll(DestinationEntity.class);
		for(DestinationEntity destEntity:getModel().getRootFileEntity().getDestinations()){
			if(allDestinations.contains(destEntity)){
				allDestinations.remove(destEntity);
			}
		}
		if (getModel().getDestinationsToAssignListModel() == null) {
			getModel().setDestinationsToAssignListModel(
					new BindingListModelList(allDestinations, false));
		} else {
			getModel().getDestinationsToAssignListModel().clear();
			getModel().getDestinationsToAssignListModel().addAll(
					allDestinations);
		}
	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}

	public void onClick$assignDestinationButton(Event e) {
		DestinationEntity selectedDestinationToAssign = getModel()
				.getDestToAssign();
		FileEntity rootFileEntity = getModel().getRootFileEntity();
		rootFileEntity.getDestinations().add(selectedDestinationToAssign);
		getCommonPersistenceService().updateEntity(rootFileEntity);
		reloadLists();
	}
	
	public void onClick$removeDestinationButton(Event e) {
		DestinationEntity assignedSelectedDestination = getModel()
				.getSelectedAssignedDestination();
		FileEntity rootFileEntity = getModel().getRootFileEntity();
		rootFileEntity.getDestinations().remove(assignedSelectedDestination);
		getCommonPersistenceService().updateEntity(rootFileEntity);
		reloadLists();
	}
	
	public class AssignedDestinationsModel extends ListModelList implements
			RowRenderer {

		private static final long serialVersionUID = 1L;

		public AssignedDestinationsModel() {
			super();
		}

		public void render(Row row, Object data) throws Exception {
			final DestinationEntity dest = (DestinationEntity) data;
			row.appendChild(new Label(dest.getName()));
//			TransmissionStatus transmissionStatus = getTransmissionStatus(
//					getDirectoryManagerModel().getSelectedFile().getFileEntity(), dest);
//			row.appendChild(new Label(transmissionStatus
//					.getTransmittedPercent().toString() + "%"));
			row.setAttribute("assignedDestination", data);
		}

	}


	public void setModel(SyncModel model) {
		this.model = model;
	}

	public SyncModel getModel() {
		return model;
	}

	public void setCommonPersistenceService(ICommonPersistenceService commonPersistenceService) {
		this.commonPersistenceService = commonPersistenceService;
	}

	public ICommonPersistenceService getCommonPersistenceService() {
		return commonPersistenceService;
	}

	public void setDirectoryManagerModel(DirectoryMananagerModel directoryManagerModel) {
		this.directoryManagerModel = directoryManagerModel;
	}

	public DirectoryMananagerModel getDirectoryManagerModel() {
		return directoryManagerModel;
	}

}
